package com.jiantong.version.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.version.impl.dao.DrugOrVaccineDao;
import com.jiantong.version.model.DrugOrVaccine;
import com.jiantong.version.service.DrugOrVaccineReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 7/9/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class DrugOrVaccineReadServiceImpl implements DrugOrVaccineReadService{

    private final DrugOrVaccineDao drugOrVaccineDao;

    @Autowired
    public DrugOrVaccineReadServiceImpl(DrugOrVaccineDao drugOrVaccineDao) {
        this.drugOrVaccineDao = drugOrVaccineDao;
    }

    /**
     * 根据类型查询 疫苗 或者 兽药的list
     *
     * @param criteria 参数
     * @return List<DrugOrVaccine>
     */
    @Override
    public Response<List<DrugOrVaccine>> queryListByType(Map<String, Object> criteria) {
        try {
            return Response.ok(drugOrVaccineDao.queryListByType(criteria));
        }catch (Exception e){
            log.error("failed to query list by type , case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to query list by type");
        }
    }

    /**
     * 根据类型查询 疫苗 或者 兽药的paging
     *
     * @param pageNo   页码
     * @param pageSize 多条
     * @param type     类型
     * @return Paging<DrugOrVaccine>
     */
    @Override
    public Response<Paging<DrugOrVaccine>> paging(Integer pageNo, Integer pageSize, Integer type) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String , Object> criteria = Maps.newHashMap();
            if(type != null){
                criteria.put("type",type);
            }
            return Response.ok(drugOrVaccineDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging drug ot vaccine , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to paging drug ot vaccine");
        }
    }

    @Override
    public Response<DrugOrVaccine> findByCode(Map<String, Object> criteria) {
        try{
            return Response.ok(drugOrVaccineDao.findByCode(criteria));
        }catch (Exception e){
            log.error("failed to find by code , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find by code");
        }
    }
}
